package com.atguigu.mySmartCampus.Service.impl;

import com.atguigu.mySmartCampus.mapper.AdminMapper;
import com.atguigu.mySmartCampus.Service.AdminService;
import com.atguigu.mySmartCampus.entity.Admin;
import com.atguigu.mySmartCampus.entity.LoginForm;
import com.atguigu.mySmartCampus.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {
    @Override
    public Admin login(LoginForm loginForm) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("name",loginForm.getUsername());
        qw.eq("password", MD5.encrypt(loginForm.getPassword()));
        System.out.println("AdminServiceImpl.login"
        +"\n--> name="+loginForm.getUsername()
        +"\n--> password="+loginForm.getPassword()
        );
        return baseMapper.selectOne(qw);
    }

    @Override
    public Admin getAdminById(Long adminId) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("id",adminId);
        return baseMapper.selectOne(qw);
    }

    @Override
    public IPage<Admin> selectAdminByOperator(Page<Admin> pageAdmin, String adminName) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(adminName)) {

            qw.like("name",adminName);
        }
        qw.orderByDesc("id");
        Page<Admin> adminPage = baseMapper.selectPage(pageAdmin, qw);
        return adminPage;
    }
}
